package com.lbt.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lbt.eduservice.entity.EduSubject;
import com.lbt.eduservice.entity.excel.SubjectData;
import com.lbt.eduservice.entity.subject.OneSubject;
import com.lbt.eduservice.entity.subject.TwoSubject;
import com.lbt.eduservice.listener.SubjectExcelListener;
import com.lbt.eduservice.mapper.EduSubjectMapper;
import com.lbt.eduservice.service.EduSubjectService;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-02-29
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try {
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    //课程分类列表（树形）
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查询出一级分类
        QueryWrapper<EduSubject> wrapper1=new QueryWrapper<>();
        wrapper1.eq("parent_id",0);
        List<EduSubject> eduSubjectsOne = baseMapper.selectList(wrapper1);
        //查询 二级分类
        QueryWrapper<EduSubject>wrapper2=new QueryWrapper<>();
        wrapper2.ne("parent_id",0);
        List<EduSubject> eduSubjectsTow = baseMapper.selectList(wrapper2);
        //循环 把二级分类添加进一级分类中，OneSubject中

        //创建一个，一级分类列表，将二级分类整合进去，然后返回一级分类列表
        List<OneSubject> oneSubjectList=new ArrayList<OneSubject>();
        for(int i=0;i<eduSubjectsOne.size();i++){
            //将所有的一级分类先加入列表
            OneSubject oneSubject=new OneSubject();
//            oneSubject.setId(eduSubjectsOne.get(i).getId());
//            oneSubject.setTitle(eduSubjectsOne.get(i).getTitle());
            BeanUtils.copyProperties(eduSubjectsOne.get(i),oneSubject);
            EduSubject eduSubject = eduSubjectsOne.get(i);
           // list.add(i,oneSubject);
            List<TwoSubject> twoSubjectList=new ArrayList<TwoSubject>();
            for(int j=0;j<eduSubjectsTow.size();j++){
                TwoSubject twoSubject=new TwoSubject();
//                twoSubject.setId(eduSubjectsTow.get(j).getId());
//                twoSubject.setTitle(eduSubjectsTow.get(j).getTitle());
                BeanUtils.copyProperties(eduSubjectsTow.get(j),twoSubject);
                //当前的二级分类属于当前一级分类的子项目，将当前的二级分类添加进一级分类
                if(eduSubject.getId().equals(eduSubjectsTow.get(j).getParentId())){
                    twoSubjectList.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoSubjectList);
            oneSubjectList.add(oneSubject);
        }
        return oneSubjectList;
    }
}
